"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const CommentSer_1 = __importDefault(require("../../services/CommentSer"));
const infoerr_1 = require("../middleware/infoerr");
exports.default = {
    getall(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                const data = yield CommentSer_1.default.getall();
                (0, infoerr_1.info)(res, "ok", data);
            }
            catch (error) {
                next(error);
            }
        });
    },
    submit(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (!req.body.content || !req.body.createdTime)
                    return (0, infoerr_1.err)(res, "参数错误", req.body);
                const data = {
                    content: req.body.content,
                    createdTime: req.body.createdTime,
                    name: req.query.name,
                    imgUrl: req.query.imgUrl,
                    zan: 0,
                    cai: 0,
                };
                const result = yield CommentSer_1.default.submit(data);
                if (!result)
                    return (0, infoerr_1.err)(res, "错误", result);
                return (0, infoerr_1.info)(res, "ok", result);
            }
            catch (error) {
                next(error);
            }
        });
    },
    update(req, res, next) {
        return __awaiter(this, void 0, void 0, function* () {
            try {
                if (+req.body.id)
                    if (req.body.zan || req.body.cai) {
                        const ispk = yield CommentSer_1.default.findpk(+req.body.id);
                        if (!ispk)
                            return (0, infoerr_1.err)(res, "找不到对应id值", req.body);
                        if (req.body.zan === "zan" && !req.body.cai) {
                            const data = {
                                zan: +ispk.zan + 1,
                            };
                            const result = yield CommentSer_1.default.update(data, +req.body.id);
                            if (result)
                                return (0, infoerr_1.info)(res, "ok");
                            return (0, infoerr_1.err)(res, "点赞失败");
                        }
                        if (!req.body.zan && req.body.cai === "cai") {
                            const data = {
                                cai: +ispk.cai + 1,
                            };
                            const result = yield CommentSer_1.default.update(data, +req.body.id);
                            if (result)
                                return (0, infoerr_1.info)(res, "ok");
                            return (0, infoerr_1.err)(res, "踩失败");
                        }
                        return (0, infoerr_1.err)(res, "点在和踩只能有一个");
                    }
                (0, infoerr_1.err)(res, "参数有误", req.body);
            }
            catch (error) {
                next(error);
            }
        });
    },
};
